package com.fellong.flash.flar.view
{
	import com.fellong.flash.flar.detector.FlarDetector;
	import com.fellong.flash.flar.util.FlarCapture;
	import com.fellong.flash.flar.util.FlarCaptureSource;
	import com.fellong.flash.flar.util.NullCapture;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	
	import org.libspark.flartoolkit.core.FLARSquare;
	import org.libspark.flartoolkit.support.pv3d.FLARCamera3D;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.stats.StatsView;
	
	public class FlarBasicView extends BasicView {
		
		private var _capture:FlarCaptureSource;
		private var _overlay:Shape = null;
		private var _stat:StatsView = null;
		
		public function FlarBasicView( green_mask:Boolean=false, debug_draw:Boolean=false, show_stat:Boolean=false, cname:String=null ) 
		{
			super(320, 240, false, true);
			
			this.green_mask = green_mask;
			this.debug_draw = debug_draw;
			this.show_stat = show_stat;
			
			try {
				_capture = new FlarCapture( 320, 240, cname );
			} catch( e:Error )
			{
				_capture = new NullCapture( "Data/nullCamera.jpg", 320, 240 );
			}
			// _capture behind viewport
			this.addChildAt( _capture as DisplayObject, this.getChildIndex( this.viewport ) );
			
			FlarDetector.init( _capture.bitmapData );

			_camera = new FLARCamera3D( FlarDetector.param );
		}		
		
		public function set green_mask( v:Boolean ):void 
		{
			if( v ) 	
			{
				this.viewport.filters = [
					new ColorMatrixFilter([
						1,  0, 0, 0, 0,
						0,  1, 0, 0, 0,
						0,  0, 1, 0, 0,
						1, -1, 1, 1, 0
					])
				];			
			} else
			{
				this.viewport.filters = null;			
			}
		}
		
		public function set debug_draw( v:Boolean ):void 
		{
			if( _overlay )
			{
				this.removeChild(_overlay);
				_overlay = null;
			}
			if( v )
			{
				_overlay = new Shape();
				this.addChild( _overlay );
			}
		}
		
		public function set show_stat( v:Boolean ):void 
		{
			if( _stat )
			{
				this.removeChild(_stat);
				_stat = null;
			}
			if( v )
			{
				_stat = new StatsView( this.renderer );
				_stat.scaleX = _stat.scaleY = 0.5;
				this.addChild( _stat );
			}
		}

		override protected function onRenderTick(event:Event = null):void
		{
			_capture.draw();

			// marker detect
			FlarDetector.detectMarkerLite();
			
			if( _overlay )
			{	/// debug draw
				var _debug_graph:Graphics = _overlay.graphics;
				_debug_graph.clear();
				
				var i:int;
				var v:Array;
				var vi:int;
				var square:FLARSquare;
				/// draw red square for all detected square
				for( i=0; i<FlarDetector.number_of_square; i++ )
				{
					square = FlarDetector.getResult(i).square;
					if(square)
					{
						///FlarDetector.getPattern( square );
						v = square.sqvertex;
						_debug_graph.lineStyle(3, 0xFF0000);
						_debug_graph.moveTo(v[3].x, v[3].y);
						for( vi = 0; vi < v.length; vi++)
						{
							_debug_graph.lineTo(v[vi].x, v[vi].y);
						}
					}
				}

				/// draw blue square for all detected marker
				var ds:Array = FlarDetector.currentDetected();
				for each ( var detector:FlarDetector in ds )
				{
					square = detector.result.square;
					if(square)
					{
						//FlarDetector.getPattern( square,16,100 );
						v = square.sqvertex;
						_debug_graph.lineStyle(3, 0x0000FF);
						_debug_graph.moveTo(v[3].x, v[3].y);
						for( vi = 0; vi < v.length; vi++)
						{
							_debug_graph.lineTo(v[vi].x, v[vi].y);
						}
					}
				}

			}
			
			super.onRenderTick(event);
		}
		
	}
}